<template>
  <div class="header-container">
    <h1 class="title">
      嗨购后台管理
    </h1>
    <div class="user-action">
      <el-dropdown trigger="click">
        <span class="el-dropdown-link">
          {{ adminname }}
          <el-icon class="el-icon--right"><arrow-down /></el-icon>
        </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item @click="logout">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<script>

import { mapState, mapActions } from 'pinia'
import { useAdminStore } from '../stores/admin-store'
export default {
  computed: {
    ...mapState(useAdminStore, ['adminname'])
  },
  methods: {
    ...mapActions(useAdminStore, ['setAdminname']),
    logout() {
      // 从 localStorage 里删除 token 和 adminname
      localStorage.removeItem('token')
      localStorage.removeItem('adminname')

      // 清空 store里的 adminname
      this.setAdminname('')

      // 跳转到 登录页
      this.$router.push('/login')
    }
  }
}
</script>
<style>
.header-container {
  border-bottom: 1px solid var(--el-border-color);
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
}
.title {
  padding: 10px 0;
  font-size:32px;
  font-weight: bold;
}
.header-container {
  cursor: pointer;
}
</style>